package com.ruoyi.web.controller.common;

import com.ruoyi.biz.model.resp.ProjectDefaultResp;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysMenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author tianl
 * @date 2025/1/7
 * @description:
 */
@RestController
@RequestMapping("/api")
public class ApiController extends BaseController {
    @Autowired
    private ISysConfigService configService;
    @Autowired
    private ISysMenuService menuService;
    @Autowired
    private ISysDeptService deptService;

    @Anonymous
    @GetMapping(value = "/title")
    public AjaxResult title() {
        String title = configService.selectConfigByKey("sys.title");
        return success("ok", StringUtils.isNotBlank(title) ? title : "四川业信集团项目协调管理系统");
    }

    @Anonymous
    @GetMapping(value = "/router")
    public AjaxResult router(String comp) {
        String path = menuService.selectPathByComp(comp);
        return success("ok", path);
    }

    @Anonymous
    @GetMapping(value = "/getProjectDefault")
    public AjaxResult getDefaultDept() {
        ProjectDefaultResp resp = new ProjectDefaultResp();
        String defaultDept = configService.selectConfigByKey("biz.project.default.dept");
        if (StringUtils.isNotBlank(defaultDept)) {
            SysDept sysDept = deptService.selectDeptByName(defaultDept);
            if (sysDept != null) {
                resp.setDefaultDept(sysDept.getDeptId());
            }
        }
        String defaultPriority = configService.selectConfigByKey("biz.project.default.priority");
        if (StringUtils.isNotBlank(defaultPriority)) {
            resp.setDefaultPriority(defaultPriority);
        }
        String defaultLevel = configService.selectConfigByKey("biz.project.default.level");
        if (StringUtils.isNotBlank(defaultLevel)) {
            resp.setDefaultLevel(defaultLevel);
        }
        String defaultContractType = configService.selectConfigByKey("biz.project.default.contractType");
        if (StringUtils.isNotBlank(defaultContractType)) {
            resp.setDefaultContractType(defaultContractType);
        }
        return success("ok", resp);
    }

    @Anonymous
    @GetMapping(value = "/getInvoiceDefault")
    public AjaxResult getInvoiceDefault() {
        String defaultInvoiceHeader = configService.selectConfigByKey("biz.invoice.header.type");
        if (StringUtils.isBlank(defaultInvoiceHeader)) {
            return success("ok", null);
        }
        return success("ok", defaultInvoiceHeader);
    }

}
